﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using ToolBar.Properties;
using ZWCAD;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.EditorInput;
using ZwSoft.ZwCAD.Geometry;

namespace ToolBar
{
    public static partial class MyMethod
    {
        public static ZcadApplication cadApp = (ZcadApplication)Application.ZcadApplication;
        /// <summary>
        /// 图片资源初始化
        /// </summary>
        public static void ImgFileInitialization()
        {
            List<string> zodiaclst = new List<string>() { "aligntobottom16", "aligntobottom32", "aligntocenter16", "aligntocenter32", "aligntoleft16", "aligntoleft32", "aligntomiddle16", "aligntomiddle32", "aligntoright16", "aligntoright32", "aligntotop16", "aligntotop32", "horizontaldistributioncenter16", "horizontaldistributioncenter32", "horizontaldistributionleft16", "horizontaldistributionleft32", "horizontaldistributionright16", "horizontaldistributionright32", "horizontaldistributionspacing16", "horizontaldistributionspacing32", "verticaldistributionBottom16", "verticaldistributionBottom32", "verticaldistributioncenter16", "verticaldistributioncenter32", "verticaldistributionspacing16", "verticaldistributionspacing32", "verticaldistributiontop16", "verticaldistributiontop32", "CenterBottom16", "CenterBottom32", "CenterMiddle16", "CenterMiddle32", "CenterTop16", "CenterTop32", "FixedCenterBottom16", "FixedCenterBottom32", "FixedCenterMiddle16", "FixedCenterMiddle32", "FixedCenterTop16", "FixedCenterTop32", "FixedLeftBottom16", "FixedLeftBottom32", "FixedLeftMiddle16", "FixedLeftMiddle32", "FixedLeftTop16", "FixedLeftTop32", "FixedRightBottom16", "FixedRightBottom32", "FixedRightMiddle16", "FixedRightMiddle32", "FixedRightTop16", "FixedRightTop32", "LeftBottom16", "LeftBottom32", "LeftMiddle16", "LeftMiddle32", "LeftTop16", "LeftTop32", "RightBottom16", "RightBottom32", "RightMiddle16", "RightMiddle32", "RightTop16", "RightTop32" };
            string localpath = $"{cadApp.Path}\\Support\\ZwCadToolBarBmp";
            Directory.CreateDirectory(localpath);
            foreach (string str in zodiaclst)
            {
                if (!File.Exists($"{localpath}\\{str}.bmp"))
                {
                    Bitmap btm = (Bitmap)Resources.ResourceManager.GetObject(str, Resources.Culture);
                    btm.Save($"{localpath}\\{str}.bmp");
                }
            }
        }
        #region 工具栏---工具栏按钮集合
        /// <summary>
        /// 对象对齐与分布工具栏按钮集合
        /// </summary>
        public static BarItem[] AlDis = new BarItem[]
        {
                new BarItem(0, "水平左对齐", "对象水平左对齐：ATL","_ATL ", false, "aligntoleft"),
                new BarItem(1, "水平居中对齐", "对象水平居中对齐：ATC", "_ATC ", false, "aligntocenter"),
                new BarItem(2, "水平右对齐", "对象水平右对齐：ATR", "_ATR ", false, "aligntoright"),

                new BarItem(3, "垂直顶对齐", "对象垂直顶对齐：ATU", "_ATU ", false, "aligntotop"),
                new BarItem(4, "垂直居中对齐", "对象垂直居中对齐：ATM", "_ATM ", false, "aligntomiddle"),
                new BarItem(5, "垂直底对齐", "对象垂直底对齐：ATB", "_ATB ", false, "aligntobottom"),

                new BarItem(6, "水平左分布", "对象水平左分布：HDL", "_HDL ", false, "horizontaldistributionleft"),
                new BarItem(7, "水平居中分布", "对象水平居中分布：HDC", "_HDC ", false, "horizontaldistributioncenter"),
                new BarItem(8, "水平右分布", "对象水平右分布：HDR", "_HDR ", false, "horizontaldistributionright"),

                new BarItem(9, "垂直顶分布", "对象垂直顶分布：VDT", "_VDT ", false, "verticaldistributiontop"),
                new BarItem(10, "垂直居中分布", "对象垂直居中分布：VDC", "_VDC ", false, "verticaldistributioncenter"),
                new BarItem(11, "垂直底分布", "对象垂直底分布：VDB", "_VDB ", false, "verticaldistributionBottom"),

                new BarItem(12, "垂直分布间距", "对象垂直分布间距：VDS", "_VDS ", false, "verticaldistributionspacing"),
                new BarItem(13, "水平分布间距", "对象水平分布间距：HDS", "_HDS ", false, "horizontaldistributionspacing"),
                new BarItem(3),
                new BarItem(7),
                new BarItem(11),
                new BarItem(15)
        };
        /// <summary>
        /// 对象双向对齐与分布工具栏按钮集合
        /// </summary>
        public static BarItem[] DouAlDis = new BarItem[]
        {
                new BarItem(0, "左上对齐", "对象左上对齐：ATLU","_ATLU ", false, "aligntoleftup"),
                new BarItem(1, "中上对齐", "对象中上对齐：ATCU", "_ATCU ", false, "aligntocenterup"),
                new BarItem(2, "右上对齐", "对象右上对齐：ATRU", "_ATRU ", false, "aligntorightup"),

                new BarItem(3, "左中对齐", "对象左中对齐：ATLC", "_ATLC ", false, "aligntoleftcenter"),
                new BarItem(4, "正中对齐", "对象正中对齐：ATMC", "_ATMC ", false, "aligntomiddlecenter"),
                new BarItem(5, "右中对齐", "对象右中对齐：ATRC", "_ATRC ", false, "aligntorightcenter"),

                new BarItem(6, "左下对齐", "对象左下对齐：ATLB", "_ATLB ", false, "aligntoleftbottom"),
                new BarItem(7, "中下对齐", "对象中下对齐：ATCB", "_ATCB ", false, "aligntocenterbottom"),
                new BarItem(8, "右下对齐", "对象右下对齐：ATRB", "_ATRB ", false, "aligntorightbottom"),

                new BarItem(12, "双向分布间距", "对象双向分布间距：DouDS", "_DouDS ", false, "doubledistributionspacing"),
                new BarItem(3),
                new BarItem(7),
                new BarItem(11)

        };
        /// <summary>
        /// 文字内部对正工具栏按钮集合
        /// </summary>
        public static BarItem[] TAI = new BarItem[]
        {
            new BarItem(0, "左上对正：TALT", "文字左上对正：TALT","_TALT ", false, "LeftTop"),
            new BarItem(1, "中上对正：TACT", "文字中上对正：TACT", "_TACT ", false, "CenterTop"),
            new BarItem(2, "右上对正：TART", "文字右上对正：TART", "_TART ", false, "RightTop"),

            new BarItem(3, "左中对正：TALM", "文字左中对正：TALM", "_TALM ", false, "LeftMiddle"),
            new BarItem(4, "正中对正：TACM", "文字正中对正：TACM", "_TACM ", false, "CenterMiddle"),
            new BarItem(5, "右中对正：TARM", "文字右中对正：TARM", "_TARM ", false, "RightMiddle"),

            new BarItem(6, "左下对正：TALB", "文字左下对正：TALB", "_TALB ", false, "LeftBottom"),
            new BarItem(7, "中下对正：TACB", "文字中下对正：TACB", "_TACB ", false, "CenterBottom"),
            new BarItem(8, "右下对正：TACR", "文字右下对正：TACR", "_TACR ", false, "RightBottom"),
            new BarItem(3),
            new BarItem(7),
        };
        public static BarItem[] TAIF = new BarItem[]
        {
            new BarItem(0, "原地左上对正：TAFLT", "文字原地左上对正：TAFLT","_TAFLT ", false, "FixedLeftTop"),
            new BarItem(1, "原地中上对正：TAFCT", "文字原地中上对正：TAFCT", "_TAFCT ", false, "FixedCenterTop"),
            new BarItem(2, "原地右上对正：TAFRT", "文字原地右上对正：TAFRT", "_TAFRT ", false, "FixedRightTop"),

            new BarItem(3, "原地左中对正：TAFLM", "文字原地左中对正：TAFLM", "_TAFLM ", false, "FixedLeftMiddle"),
            new BarItem(4, "原地正中对正：TAFCM", "文字原地正中对正：TAFCM", "_TAFCM ", false, "FixedCenterMiddle"),
            new BarItem(5, "原地右中对正：TAFRM", "文字原地右中对正：TAFRM", "_TAFRM ", false, "FixedRightMiddle"),

            new BarItem(6, "原地左下对正：TAFLB", "文字原地左下对正：TAFLB", "_TAFLB ", false, "FixedLeftBottom"),
            new BarItem(7, "原地中下对正：TAFCB", "文字原地中下对正：TAFCB", "_TAFCB ", false, "FixedCenterBottom"),
            new BarItem(8, "原地右下对正：TAFCR", "文字原地右下对正：TAFCR", "_TAFCR ", false, "FixedRightBottom"),
            new BarItem(3),
            new BarItem(7),
        };
        //加载工具栏
        public static void CreatToolBar(string name, params BarItem[] toolitem)
        {
            ZcadMenuGroup currMenuGroup = cadApp.MenuGroups.Item(0);
            ZcadToolbar newToolBar;
            try
            {
                newToolBar = currMenuGroup.Toolbars.Item(name);
            }
            catch
            {
                newToolBar = currMenuGroup.Toolbars.Add(name);
                foreach (BarItem item in toolitem)
                {
                    ZcadToolbarItem newButton = null;
                    if (item.isbtn)
                    {
                        newButton = newToolBar.AddToolbarButton(item.Index, item.Name, item.HelpString, item.Macro, item.FlyoutButton);
                        newButton.SetBitmaps($"ZwCadToolBarBmp\\{item.SmallIconName}16.bmp", $"ZwCadToolBarBmp\\{item.LargeIconName}32.bmp");
                    }
                    else
                    {
                        newToolBar.AddSeparator(item.Index);
                    }
                }
                currMenuGroup.Save(ZcMenuFileType.zcMenuFileSource);
            }
            //工具栏靠右边停靠
            //newToolBar.Dock(ZcToolbarDockStatus.zcToolbarDockRight);
            //显示工具条
            if (!newToolBar.Visible) newToolBar.Visible = true;
        }
        #endregion
        public static void Mymethod()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            ed.DrawVector(new Point3d(0, 0, 0), new Point3d(100, 0, 0), 1, true);
            ed.DrawVector(new Point3d(100, 0, 0), new Point3d(100, 100, 0), 1, true);
            ed.DrawVector(new Point3d(100, 100, 0), new Point3d(0, 100, 0), 1, true);
            ed.DrawVector(new Point3d(0, 100, 0), new Point3d(0, 0, 0), 1, true);
        }

        private static void ed_PointMonitor(object sender, PointMonitorEventArgs e)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.DrawVector(new Point3d(0, 0, 0), new Point3d(100, 0, 0), 1, true);
            ed.DrawVector(new Point3d(100, 0, 0), new Point3d(100, 100, 0), 1, true);
            ed.DrawVector(new Point3d(100, 100, 0), new Point3d(0, 100, 0), 1, true);
            ed.DrawVector(new Point3d(0, 100, 0), new Point3d(0, 0, 0), 1, true);
        }


        #region 图形功能---对象对齐与分布、文字内部对正
        /// <summary>
        /// 对象对齐
        /// </summary>
        /// <param name="alanddis">对齐方式</param>
        public static void EntityAlign(AlAndDis alanddis)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.DrawVector(new Point3d(0, 0, 0), new Point3d(100, 0, 0), 1, true);
            ed.DrawVector(new Point3d(100, 0, 0), new Point3d(100, 100, 0), 1, true);
            ed.DrawVector(new Point3d(100, 100, 0), new Point3d(0, 100, 0), 1, true);
            ed.DrawVector(new Point3d(0, 100, 0), new Point3d(0, 0, 0), 1, true);
            using (Document doc = Application.DocumentManager.MdiActiveDocument)
            {
                using (DocumentLock dl = doc.LockDocument())
                {
                    ObjectId[] ids = new ObjectId[0], alids;
                    Point3d selpt1 = new Point3d(), selpt2 = new Point3d();
                    //声明一个获取点的提示类
                    PromptPointResult ppr; PromptPointOptions ppo;
                    bool isc = true, iselpt1 = false, isent = true;
                    while (isc)
                    {
                        if (isent)
                        {
                            ppo = new PromptPointOptions("\n请选择需要对齐的【对象】或【选择集(S)】:");
                            ppo.Keywords.Add("S");
                            if (iselpt1)
                            {
                                ppo.BasePoint    = selpt1;
                                ppo.UseBasePoint = true;
                            }
                        }
                        else
                        {
                            ppo = new PromptPointOptions("\n请选择需要对齐的【选择集】或【对象(E)】:");
                            ppo.Keywords.Add("E");//添加字符，使得相应的字符按建有效
                            if (iselpt1)
                            {
                                ppo.BasePoint    = selpt1;
                                ppo.UseBasePoint = true;
                            }
                        }
                        ppo.AppendKeywordsToMessage = false;//取消系统自动的关键字显示
                        ppo.AllowNone = true;               //使回车和空格键有效
                        ppr = ed.GetPoint(ppo);
                        if (ppr.Status == PromptStatus.Cancel) return;
                        if (ppr.Status == PromptStatus.None) return;
                        if (ppr.Status == PromptStatus.OK)
                        {
                            if (iselpt1)
                            {
                                selpt2 = ppr.Value;
                                isc    = false;
                            }
                            else
                            {
                                selpt1  = ppr.Value;
                                iselpt1 = true;
                            }
                        }
                        if (ppr.Status == PromptStatus.Keyword)
                        {
                            switch (ppr.StringResult)
                            {
                                case "E":
                                    isent = true;
                                    break;
                                case "S":
                                    isent = false;
                                    break;
                            }
                        }
                    }
                    PromptSelectionResult psr = ed.SelectCrossingWindow(selpt1, selpt2);
                    if (psr.Status == PromptStatus.OK) ids = psr.Value.GetObjectIds();
                    else return;
                    PromptSelectionOptions pso = new PromptSelectionOptions();
                    pso.AllowDuplicates = false;                                                                         //重复选择
                    pso.MessageForAdding = "\n请选择对齐对象【选择单个对象：对齐于对象；选择多个对象：对齐于选择集】："; //"\n请选择对齐对象或[对象对齐(E)/选择集对齐(S)]："
                    psr = ed.GetSelection(pso);
                    if (psr.Status == PromptStatus.OK) alids = psr.Value.GetObjectIds();
                    else return;
                    //PromptEntityResult per = ed.GetEntity("\n请选择对齐对象：");
                    //if (per.Status == PromptStatus.OK) alids = per.ObjectId;
                    //else return;
                    using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                    {
                        //打开块表
                        BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
                        //打开块表记录
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                        Extents3d alex = new Extents3d(), ex = new Extents3d();
                        Point3d alpt = new Point3d();
                        double alx = 0, aly = 0;
                        #region 获取实体集合的范围
                        alex = ((Entity)tr.GetObject(alids[0], OpenMode.ForWrite)).GeometricExtents;
                        foreach (ObjectId id in alids)
                        {
                            alex.AddExtents(((Entity)tr.GetObject(id, OpenMode.ForWrite)).GeometricExtents);
                        }
                        #endregion
                        switch (alanddis)
                        {
                            //===================
                            #region AlignToLeft 水平左对齐
                            case AlAndDis.AlignToLeft:
                                alx = alex.MinPoint.X;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(alx, pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToCenter 水平居中对齐
                            case AlAndDis.AlignToCenter:
                                alx = GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint).X;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint);
                                    Vector3d vec = new Point3d(alx, pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToRight 水平右对齐
                            case AlAndDis.AlignToRight:
                                alx = alex.MaxPoint.X;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MaxPoint;
                                    Vector3d vec = new Point3d(alx, pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToTop 垂直顶对齐
                            case AlAndDis.AlignToTop:
                                aly = alex.MaxPoint.Y;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MaxPoint;
                                    Vector3d vec = new Point3d(pt.X, aly, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToMiddle 垂直居中对齐
                            case AlAndDis.AlignToMiddle:
                                aly = GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint).Y;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint);
                                    Vector3d vec = new Point3d(pt.X, aly, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToBottom 垂直底对齐
                            case AlAndDis.AlignToBottom:
                                aly = alex.MinPoint.Y;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(pt.X, aly, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            //===================
                            #region AlignToLeftUp 左上对齐
                            case AlAndDis.AlignToLeftUp:
                                alpt = new Point3d(alex.MinPoint.X, alex.MaxPoint.Y, 0.0);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = new Point3d(ex.MinPoint.X, ex.MaxPoint.Y, 0.0);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToCenterUp 中上对齐
                            case AlAndDis.AlignToCenterUp:
                                alpt = new Point3d(GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint).X, alex.MaxPoint.Y, 0.0);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = new Point3d(GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint).X, ex.MaxPoint.Y, 0.0);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToRightUp 右上对齐
                            case AlAndDis.AlignToRightUp:
                                alpt = alex.MaxPoint;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MaxPoint;
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToLeftCenter 左中对齐
                            case AlAndDis.AlignToLeftCenter:
                                alpt = new Point3d(alex.MinPoint.X, GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint).Y, 0.0);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = new Point3d(ex.MinPoint.X, GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint).Y, 0.0);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToMiddleCenter 正中对齐
                            case AlAndDis.AlignToMiddleCenter:
                                alpt = GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToRightCenter 右中对齐
                            case AlAndDis.AlignToRightCenter:
                                alpt = new Point3d(alex.MaxPoint.X, GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint).Y, 0.0);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = new Point3d(ex.MaxPoint.X, GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint).Y, 0.0);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToLeftBottom 左下对齐
                            case AlAndDis.AlignToLeftBottom:
                                alpt = alex.MinPoint;
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToCenterBottom 中下对齐
                            case AlAndDis.AlignToCenterBottom:
                                alpt = new Point3d(GetCenterBetweenTwoPoint(alex.MinPoint, alex.MaxPoint).X, alex.MinPoint.Y, 0.0);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = new Point3d(GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint).X, ex.MinPoint.Y, 0.0);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region AlignToRightBottom 右下对齐
                            case AlAndDis.AlignToRightBottom:
                                alpt = new Point3d(alex.MaxPoint.X, alex.MinPoint.Y, 0.0);
                                foreach (ObjectId id in ids)
                                {
                                    Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = new Point3d(ex.MaxPoint.X, ex.MinPoint.Y, 0.0);
                                    Vector3d vec = alpt.GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            default:
                                break;
                        }
                        tr.Commit();
                    }
                }
            }
        }
        /// <summary>
        /// 对象分布
        /// </summary>
        /// <param name="alanddis">分布方式</param>
        public static void EntityDistribution(AlAndDis alanddis)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            using (Document doc = Application.DocumentManager.MdiActiveDocument)
            {
                using (doc.LockDocument())
                {
                    ObjectId[] ids;
                    PromptSelectionOptions pso = new PromptSelectionOptions();
                    pso.MessageForAdding = "\n请选择需要分布的对象：";
                    PromptSelectionResult psr = ed.GetSelection(pso);
                    if (psr.Status == PromptStatus.OK) ids = psr.Value.GetObjectIds();
                    else return;
                    using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                    {
                        //打开块表
                        BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
                        //打开块表记录
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                        Extents3d ex = new Extents3d(), exsta = new Extents3d(), exend = new Extents3d();
                        double dis = 0, alsx = 0, alsy = 0;
                        switch (alanddis)
                        {
                            #region HorizontalDistributionLeft 水平左分布
                            case AlAndDis.HorizontalDistributionLeft:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint.X).ToArray();
                                exsta = ((Entity)ids[0].GetObject(OpenMode.ForRead)).GeometricExtents;
                                exend = ((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead)).GeometricExtents;
                                dis = (exend.MinPoint.X - exsta.MinPoint.X) / (ids.Length - 1);
                                alsx = exsta.MinPoint.X;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = ent.GeometricExtents;
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(alsx + (i * dis), pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region HorizontalDistributionCenter 水平居中分布
                            case AlAndDis.HorizontalDistributionCenter:
                                ids = ids.OrderBy(id => GetCenterBetweenTwoPoint(GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint, GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MaxPoint).X).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                alsx = GetCenterBetweenTwoPoint(exsta.MinPoint, exsta.MaxPoint).X;
                                dis = (GetCenterBetweenTwoPoint(exend.MinPoint, exend.MaxPoint).X - alsx) / (ids.Length - 1);
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint);
                                    Vector3d vec = new Point3d(alsx + (i * dis), pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region HorizontalDistributionRight 水平右分布
                            case AlAndDis.HorizontalDistributionRight:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MaxPoint.X).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = (exend.MaxPoint.X - exsta.MaxPoint.X) / (ids.Length - 1);
                                alsx = exsta.MaxPoint.X;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MaxPoint;
                                    Vector3d vec = new Point3d(alsx + (i * dis), pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion

                            #region VerticalDistributionTop 垂直顶分布
                            case AlAndDis.VerticalDistributionTop:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MaxPoint.Y).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = (exend.MaxPoint.Y - exsta.MaxPoint.Y) / (ids.Length - 1);
                                alsy = exsta.MaxPoint.Y;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MaxPoint;
                                    Vector3d vec = new Point3d(pt.X, alsy + (i * dis), pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region VerticalDistributionCenter 垂直居中分布
                            case AlAndDis.VerticalDistributionCenter:
                                ids = ids.OrderBy(id => GetCenterBetweenTwoPoint(GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint, GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MaxPoint).Y).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                alsy = GetCenterBetweenTwoPoint(exsta.MinPoint, exsta.MaxPoint).Y;
                                dis = (GetCenterBetweenTwoPoint(exend.MinPoint, exend.MaxPoint).Y - alsy) / (ids.Length - 1);
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = GetCenterBetweenTwoPoint(ex.MinPoint, ex.MaxPoint);
                                    Vector3d vec = new Point3d(pt.X, alsy + (i * dis), pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion
                            #region VerticalDistributionBottom 垂直底分布
                            case AlAndDis.VerticalDistributionBottom:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint.Y).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = (exend.MinPoint.Y - exsta.MinPoint.Y) / (ids.Length - 1);
                                alsy = exsta.MinPoint.Y;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(pt.X, alsy + (i * dis), pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                }
                                break;
                            #endregion

                            #region HorizontalDistributionSpacing 水平分布间距
                            case AlAndDis.HorizontalDistributionSpacing:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint.X).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = exend.MaxPoint.X - exsta.MinPoint.X;
                                Array.ForEach(ids, (id) =>
                                {
                                    Extents3d lsex = GetEntExtents((Entity)id.GetObject(OpenMode.ForRead));
                                    dis -= lsex.MaxPoint.X - lsex.MinPoint.X;
                                });
                                dis /= ids.Length - 1;
                                alsx = exsta.MaxPoint.X;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(alsx + dis, pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                    alsx = alsx + dis + (ex.MaxPoint.X - ex.MinPoint.X);
                                }
                                break;
                            #endregion
                            #region VerticalDistributionSpacing 垂直分布间距
                            case AlAndDis.VerticalDistributionSpacing:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint.Y).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = exend.MaxPoint.Y - exsta.MinPoint.Y;
                                Array.ForEach(ids, (id) =>
                                {
                                    Extents3d lsex = GetEntExtents((Entity)id.GetObject(OpenMode.ForRead));
                                    dis -= lsex.MaxPoint.Y - lsex.MinPoint.Y;
                                });
                                dis /= ids.Length - 1;
                                alsy = exsta.MaxPoint.Y;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(pt.X, alsy + dis, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                    alsy = alsy + dis + (ex.MaxPoint.Y - ex.MinPoint.Y);
                                }
                                break;
                            #endregion

                            #region DoubleDistributionSpacing 双向分布间距
                            case AlAndDis.DoubleDistributionSpacing:
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint.X).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = exend.MaxPoint.X - exsta.MinPoint.X;
                                Array.ForEach(ids, (id) =>
                                {
                                    Extents3d lsex = GetEntExtents((Entity)id.GetObject(OpenMode.ForRead));
                                    dis -= lsex.MaxPoint.X - lsex.MinPoint.X;
                                });
                                dis /= ids.Length - 1;
                                alsx = exsta.MaxPoint.X;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(alsx + dis, pt.Y, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                    alsx = alsx + dis + (ex.MaxPoint.X - ex.MinPoint.X);
                                }
                                ids = ids.OrderBy(id => GetEntExtents((Entity)id.GetObject(OpenMode.ForRead)).MinPoint.Y).ToArray();
                                exsta = GetEntExtents((Entity)ids[0].GetObject(OpenMode.ForRead));
                                exend = GetEntExtents((Entity)ids[ids.Length - 1].GetObject(OpenMode.ForRead));
                                dis = exend.MaxPoint.Y - exsta.MinPoint.Y;
                                Array.ForEach(ids, (id) =>
                                {
                                    Extents3d lsex = GetEntExtents((Entity)id.GetObject(OpenMode.ForRead));
                                    dis -= lsex.MaxPoint.Y - lsex.MinPoint.Y;
                                });
                                dis /= ids.Length - 1;
                                alsy = exsta.MaxPoint.Y;
                                for (int i = 1; i < ids.Length - 1; i++)
                                {
                                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
                                    ex = GetEntExtents(ent);
                                    Point3d pt = ex.MinPoint;
                                    Vector3d vec = new Point3d(pt.X, alsy + dis, pt.Z).GetVectorTo(pt);
                                    Matrix3d mt = Matrix3d.Displacement(vec);
                                    ent.TransformBy(mt);
                                    alsy = alsy + dis + (ex.MaxPoint.Y - ex.MinPoint.Y);
                                }
                                break;
                            #endregion
                            default:
                                break;
                        }
                        tr.Commit();
                    }
                }
            }
        }

        public static List<PtId> tlst = new List<PtId>();
        public struct PtId
        {
            public Point3d pt;
            public ObjectId id;

            public PtId(Point3d pt, ObjectId id)
            {
                this.pt = pt;
                this.id = id;
            }
        }
        /// <summary>
        /// 文字内部对正
        /// </summary>
        /// <param name="textalin">对正方式</param>
        public static void TextAlignIn(TextAlIn textalin, bool IsFixed = false)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            using (Document doc = Application.DocumentManager.MdiActiveDocument)
            {
                using (doc.LockDocument())
                {
                    SelectionSet ss = SelectOnScreen(new TypedValue[] { new TypedValue(0, "*TEXT") });
                    if (ss == null) return;
                    using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                    {
                        BlockTableRecord btr = tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                        ObjectId[] ids = ss.GetObjectIds();
                        foreach (ObjectId id in ids)
                        {
                            Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
                            if ("MText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
                            {
                                MText mtxt = (MText)ent;
                                Point3d basspt;
                                if (IsFixed) basspt = GetEntExtents(mtxt).MinPoint;
                                else basspt = mtxt.GeometricExtents.MinPoint;
                                switch (textalin)
                                {
                                    case TextAlIn.TextAlignmentLeftTop:
                                        mtxt.Attachment = AttachmentPoint.TopLeft;
                                        break;
                                    case TextAlIn.TextAlignmentCenterTop:
                                        mtxt.Attachment = AttachmentPoint.TopCenter;
                                        break;
                                    case TextAlIn.TextAlignmentRightTop:
                                        mtxt.Attachment = AttachmentPoint.TopRight;
                                        break;
                                    case TextAlIn.TextAlignmentLeftMiddle:
                                        mtxt.Attachment = AttachmentPoint.MiddleLeft;
                                        break;
                                    case TextAlIn.TextAlignmentMiddle:
                                        mtxt.Attachment = AttachmentPoint.MiddleCenter;
                                        break;
                                    case TextAlIn.TextAlignmentRightMiddle:
                                        mtxt.Attachment = AttachmentPoint.MiddleRight;
                                        break;
                                    case TextAlIn.TextAlignmentLeftBottom:
                                        mtxt.Attachment = AttachmentPoint.BottomLeft;
                                        break;
                                    case TextAlIn.TextAlignmentCenterBottom:
                                        mtxt.Attachment = AttachmentPoint.BottomCenter;
                                        break;
                                    case TextAlIn.TextAlignmentRightBottom:
                                        mtxt.Attachment = AttachmentPoint.BottomRight;
                                        break;
                                }
                                Vector3d vec;
                                if (IsFixed) vec = basspt.GetVectorTo(GetEntExtents(mtxt).MinPoint);
                                else vec = basspt.GetVectorTo(mtxt.GeometricExtents.MinPoint);
                                Matrix3d mt = Matrix3d.Displacement(vec);
                                mtxt.TransformBy(mt);
                            }
                            else if ("DBText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
                            {
                                DBText dbtxt = (DBText)ent;
                                Point3d basspt = dbtxt.Position;
                                //Point3d basspt = dbtxt.GeometricExtents.MinPoint;
                                switch (textalin)
                                {
                                    case TextAlIn.TextAlignmentLeftTop:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextLeft;
                                        dbtxt.VerticalMode = TextVerticalMode.TextTop;
                                        break;
                                    case TextAlIn.TextAlignmentCenterTop:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextCenter;
                                        dbtxt.VerticalMode = TextVerticalMode.TextTop;
                                        break;
                                    case TextAlIn.TextAlignmentRightTop:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextRight;
                                        dbtxt.VerticalMode = TextVerticalMode.TextTop;
                                        break;
                                    case TextAlIn.TextAlignmentLeftMiddle:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextLeft;
                                        dbtxt.VerticalMode = TextVerticalMode.TextVerticalMid;
                                        break;
                                    case TextAlIn.TextAlignmentMiddle:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextMid;
                                        break;
                                    case TextAlIn.TextAlignmentRightMiddle:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextRight;
                                        dbtxt.VerticalMode = TextVerticalMode.TextVerticalMid;
                                        break;
                                    case TextAlIn.TextAlignmentLeftBottom:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextLeft;
                                        dbtxt.VerticalMode = TextVerticalMode.TextBase;
                                        break;
                                    case TextAlIn.TextAlignmentCenterBottom:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextCenter;
                                        dbtxt.VerticalMode = TextVerticalMode.TextBase;
                                        break;
                                    case TextAlIn.TextAlignmentRightBottom:
                                        dbtxt.HorizontalMode = TextHorizontalMode.TextRight;
                                        dbtxt.VerticalMode = TextVerticalMode.TextBase;
                                        break;
                                }
                                if (dbtxt.HorizontalMode != TextHorizontalMode.TextLeft || dbtxt.VerticalMode != TextVerticalMode.TextBase)
                                {
                                    dbtxt.AlignmentPoint = basspt;
                                }
                                if (IsFixed) tlst.Add(new PtId(basspt, id));
                                //if (IsFixed)
                                //{
                                //    Vector3d vec = basspt.GetVectorTo(dbtxt.GeometricExtents.MinPoint);
                                //    Matrix3d mt = Matrix3d.Displacement(vec);
                                //    dbtxt.TransformBy(mt);
                                //}
                            }
                        }
                        tr.Commit();
                    }
                    if (IsFixed && tlst.Count > 0)
                    {
                        using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                        {
                            BlockTableRecord btr = tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                            foreach (PtId pe in tlst)
                            {
                                DBText dbtxt = (DBText)pe.id.GetObject(OpenMode.ForWrite);
                                Vector3d vec = pe.pt.GetVectorTo(dbtxt.Position);
                                Matrix3d mt = Matrix3d.Displacement(vec);
                                dbtxt.TransformBy(mt);
                            }
                            tr.Commit();
                        }
                    }
                }
            }
        }
        #endregion
        #region 外部方法
        /// <summary>
        /// 获取图元包围框
        /// </summary>
        /// <param name="ent">Entity图元对象</param>
        /// <returns>返回左下右上两点的Extents3d对象</returns>
        private static Extents3d GetEntExtents(Entity ent)
        {
            if ("MText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
            {
                MText mtxt = (MText)ent;
                double width = mtxt.ActualWidth;
                double height = mtxt.ActualHeight;
                double wl;
                switch (mtxt.Attachment)
                {
                    case AttachmentPoint.TopCenter:
                    case AttachmentPoint.MiddleCenter:
                    case AttachmentPoint.BottomCenter:
                        wl = width * -0.5;
                        break;
                    case AttachmentPoint.TopRight:
                    case AttachmentPoint.MiddleRight:
                    case AttachmentPoint.BottomRight:
                        wl = -width;
                        break;
                    default:
                        wl = 0.0;
                        break;
                }
                double hb;
                switch (mtxt.Attachment)
                {
                    case AttachmentPoint.TopLeft:
                    case AttachmentPoint.TopCenter:
                    case AttachmentPoint.TopRight:
                        hb = -height;
                        break;
                    case AttachmentPoint.MiddleLeft:
                    case AttachmentPoint.MiddleCenter:
                    case AttachmentPoint.MiddleRight:
                        hb = height * -0.5;
                        break;
                    default:
                        hb = 0.0;
                        break;
                }
                double wr = width + wl;
                double ht = height + hb;
                double degree = mtxt.Rotation;
                Point3d center = mtxt.Location;
                Point2d ptlb = new Point2d(center.X + wl, center.Y + hb);
                Point2d ptrt = new Point2d(center.X + wr, center.Y + ht);
                //声明多段线
                Polyline pline = new Polyline();
                //计算矩形的四个顶点
                Point2d p1 = new Point2d(Math.Min(ptlb.X, ptrt.X), Math.Min(ptlb.Y, ptrt.Y));
                Point2d p2 = new Point2d(Math.Max(ptlb.X, ptrt.X), Math.Min(ptlb.Y, ptrt.Y));
                Point2d p3 = new Point2d(Math.Max(ptlb.X, ptrt.X), Math.Max(ptlb.Y, ptrt.Y));
                Point2d p4 = new Point2d(Math.Min(ptlb.X, ptrt.X), Math.Max(ptlb.Y, ptrt.Y));
                //添加多段线的顶点
                pline.AddVertexAt(0, p1, 0, 0, 0);
                pline.AddVertexAt(1, p2, 0, 0, 0);
                pline.AddVertexAt(2, p3, 0, 0, 0);
                pline.AddVertexAt(3, p4, 0, 0, 0);
                //闭合多段线
                pline.Closed = true;
                pline.TransformBy(Matrix3d.Rotation(degree, Vector3d.ZAxis, center));
                return pline.GeometricExtents;
            }
            return ent.GeometricExtents;
        }
        /// <summary>
        /// 获取实体集合的范围
        /// </summary>
        /// <param name="ids">图元ObjectId数组</param>
        /// <returns>实体集合的范围</returns>
        public static Extents3d GetExtents(this ObjectId[] ids)
        {
            Extents3d ext = new Extents3d();
            if (ids.Length > 0)
            {
                using (Document doc = Application.DocumentManager.MdiActiveDocument)
                {
                    using (doc.LockDocument())
                    {
                        using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                        {
                            BlockTableRecord btr = tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
                            ext = ((Entity)tr.GetObject(ids[0], OpenMode.ForWrite)).GeometricExtents;
                            foreach (ObjectId id in ids)
                            {
                                ext.AddExtents(((Entity)tr.GetObject(id, OpenMode.ForWrite)).GeometricExtents);
                            }
                        }
                    }
                }
            }
            return ext;
        }
        /// <summary>
        /// 通过组码获取对象
        /// </summary>
        /// <param name="value">组码数组</param>
        /// <returns>选择集</returns>
        public static SelectionSet SelectOnScreen(TypedValue[] value)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionOptions pso = new PromptSelectionOptions
            {
                RejectObjectsOnLockedLayers = true
            };
            SelectionFilter filter = new SelectionFilter(value);
            PromptSelectionResult psr = ed.GetSelection(pso, filter);
            if (psr.Status == PromptStatus.OK)
            {
                return psr.Value;
            }
            return null;
        }
        /// <summary>
        /// 获取两点的中点
        /// </summary>
        /// <param name="point1">第一点</param>
        /// <param name="point2">第二点</param>
        /// <returns>返回两点的中点</returns>
        public static Point3d GetCenterBetweenTwoPoint(Point3d point1, Point3d point2)
        {
            return new Point3d((point1.X + point2.X) / 2.0, (point1.Y + point2.Y) / 2.0, (point1.Z + point2.Z) / 2.0);
        }
        #endregion
        #region 对齐与分布、对正枚举
        /// <summary>
        /// 对象对齐与分布枚举
        /// </summary>
        public enum AlAndDis
        {
            AlignToLeft,
            AlignToCenter,
            AlignToRight,

            AlignToTop,
            AlignToBottom,
            AlignToMiddle,

            HorizontalDistributionLeft,
            HorizontalDistributionCenter,
            HorizontalDistributionRight,

            VerticalDistributionTop,
            VerticalDistributionBottom,
            VerticalDistributionCenter,

            HorizontalDistributionSpacing,
            VerticalDistributionSpacing,

            AlignToLeftUp,
            AlignToCenterUp,
            AlignToRightUp,

            AlignToLeftCenter,
            AlignToMiddleCenter,
            AlignToRightCenter,

            AlignToLeftBottom,
            AlignToCenterBottom,
            AlignToRightBottom,

            DoubleDistributionSpacing
        }
        /// <summary>
        /// 文字内部对正枚举
        /// </summary>
        public enum TextAlIn
        {
            TextAlignmentLeftTop,
            TextAlignmentCenterTop,
            TextAlignmentRightTop,

            TextAlignmentLeftMiddle,
            TextAlignmentMiddle,
            TextAlignmentRightMiddle,

            TextAlignmentLeftBottom,
            TextAlignmentCenterBottom,
            TextAlignmentRightBottom,

        }
        #endregion
    }
}
